package com.mhy.utils.toast;


import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.StringRes;
import androidx.lifecycle.LifecycleOwner;

import java.nio.charset.Charset;

/**
 * Created By Mahongyin
 * Date    2020/11/18 11:26
 */

public class ToastUtils {
    public final int LENGTH_SHORT = 3000;
    public final int LENGTH_LONG = 5000;
    private WToast toast;
    private static ToastUtils toastUtils;

    public static ToastUtils getInstance() {
        if (toastUtils == null) {
            synchronized (ToastUtils.class) {
                toastUtils = new ToastUtils();
            }
        }
        return toastUtils;
    }

    private WToast makeText(Context context, CharSequence msg, int duration) {
        hide();
        toast = null;
        toast = WToast.makeText(context, msg, duration);//创建Wtoast实例
        return toast;
    }

    /**
     * 10字内3秒    5秒
     *
     * @param context
     * @param msg
     */
    public void showToast(Context context, CharSequence msg) {
        int duration;
        if (msg.toString().getBytes(Charset.forName("GBK")).length < 20) {
            duration = LENGTH_SHORT;
        } else {
            duration = LENGTH_LONG;
        }
        makeText(context, msg, duration).show();
    }

    public void showToast(Context context, @StringRes int strRes) {
        String msg = context.getResources().getString(strRes);
        showToast(context, msg);
    }

    /**
     * 居中显示 ，自定义时长
     *
     * @param context
     * @param msg
     * @param duration
     */
    public void showToast(Context context, CharSequence msg, int duration) {
        makeText(context, msg, duration).show();
    }

    public void showToast(Context context, @StringRes int strRes, int duration) {
        String msg = context.getResources().getString(strRes);
        showToast(context, msg, duration);
    }

    private static Toast mToast;
    //默认的xOffset
    private static int sDefaultXOffset;
    //默认的yOffset
    private static int sDefaultYOffset;

    /**
     * 底部显示 ，自定义时长
     *
     * @param context
     * @param msg
     * @param duration
     */

    public void showBottomToast(Context context, CharSequence msg, int duration) {
        if (mToast == null) {
            //创建Toast单例，并保存默认的xOffset和yOffset
            mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
            sDefaultXOffset = mToast.getXOffset();
            sDefaultYOffset = mToast.getYOffset();
        }
        WToast toast = makeText(context, msg, duration);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, sDefaultXOffset, sDefaultYOffset);
        toast.show();
    }

    public void showBottomToast(Context context, @StringRes int strRes, int duration) {
        String msg = context.getResources().getString(strRes);
        showBottomToast(context, msg, duration);
    }

    /**
     * 顶部显示
     *
     * @param context
     * @param msg
     * @param duration
     */
    public void showTopToast(Context context, CharSequence msg, int duration) {
        WToast toast = makeText(context, msg, duration);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 0);
        toast.show();
    }

    public void showTopToast(Context context, @StringRes int strRes, int duration) {
        String msg = context.getResources().getString(strRes);
        showTopToast(context, msg, duration);
    }


    /**
     * 自定义时长 自定义位置
     *
     * @param context
     * @param msg
     * @param duration
     */
    public void showToast(Context context, CharSequence msg, int duration, int gravity, int xOffset, int yOffset) {
        toast = makeText(context, msg, duration);
        toast.setGravity(gravity, xOffset, yOffset);
        toast.show();
        if (context instanceof LifecycleOwner) {
            ((LifecycleOwner) context).getLifecycle().addObserver(toast);
        }
    }

    public void showToast(Context context, @StringRes int strRes, int duration, int gravity, int xOffset, int yOffset) {
        String msg = context.getResources().getString(strRes);
        showToast(context, msg, duration, gravity, xOffset, yOffset);
    }

    /**
     * 自定义view 自定义时长 自定义位置
     *
     * @param context
     * @param duration
     */
    public void showToast(Context context, View view, int duration, int gravity, int xOffset, int yOffset) {
        toast = makeText(context, "", duration);
        toast.setGravity(gravity, xOffset, yOffset);
        toast.setView(view);
        toast.show();
        if (context instanceof LifecycleOwner) {
            ((LifecycleOwner) context).getLifecycle().addObserver(toast);
        }
    }

    /**
     * 隐藏toast
     */
    private void hide() {
        if (toast != null) {
            toast.hide();
        }
    }

}
